<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Initialization</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_user_guide_init.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Initialization </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_user_guide_init_lib_init">Library Initialization</a></li>
<li class="level1"><a href="#section_user_guide_init_lib_deinit">Library Deinitialization and Cleanup</a></li>
<li class="level1"><a href="#section_user_guide_enumerate_hw">Manually Enumerating CAN channels</a></li>
</ul>
</div>
<div class="textblock"><h1><a class="anchor" id="section_user_guide_init_lib_init"></a>
Library Initialization</h1>
<p>The first call to the library must be a call to <a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a>. This function will initialize the CANlib library and enumerate all currently available CAN channels. It is sufficient to call this routine once. Calling it more than once will have no effect.</p>
<p><b>Example</b> This initializes the CANlib library.</p>
<div class="fragment"><div class="line"><a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div></div><!-- fragment --><h1><a class="anchor" id="section_user_guide_init_lib_deinit"></a>
Library Deinitialization and Cleanup</h1>
<p>Strictly speaking it is not necessary to clean up anything before terminating the application. If the application quits unexpectedly, the device driver will ensure the CAN controller is deactivated and the driver will also ensure the firmware (if any) is left in a consistent state.</p>
<p>To deinitialize the library in an orderly fashion you may want to call <a class="el" href="group___c_a_n.html#ga304cb3a7bc2874c1f8ad361a911bcd5f">canWriteSync()</a> with a short timeout for each open handle before closing them with <a class="el" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose()</a>, to ensure the transmit queues are empty.</p>
<dl class="section note"><dt>Note</dt><dd>When calling <a class="el" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary()</a>, all previously opened CAN handles will be closed and invalidated.</dd></dl>
<p>If you want to reinitialize the library from scratch in an application that already has initialized the library, call <a class="el" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary()</a>. You can then start afresh by calling <a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a> again.</p>
<p><b>Example</b> This deinitializes the CANlib library.</p>
<div class="fragment"><div class="line">stat = <a class="code" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary</a>();</div><div class="line"> <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">     printf(<span class="stringliteral">&quot;Unload library failed.&quot;</span>);</div><div class="line"> }</div></div><!-- fragment --><h1><a class="anchor" id="section_user_guide_enumerate_hw"></a>
Manually Enumerating CAN channels</h1>
<p>The function <a class="el" href="group__can__general.html#ga2cf37192c855ce901fc0d69bfe72fbd5">canEnumHardwareEx()</a> scans all currently connected devices and creates a completely new set of CANlib channel numbers, while still keeping all currently opened channel handles valid and usable. This can be viewed upon as a replacement for calling the function <a class="el" href="group__can__general.html#ga0d1c0e54ea20c3e3b328a32eb10c7b47">canUnloadLibrary()</a> followed by <a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a> which do invalidate all open channel handles.</p>
<p>One thing to keep in mind when using this functionality is to never track devices based on their CANlib channel number, since this number may change anytime <a class="el" href="group__can__general.html#ga2cf37192c855ce901fc0d69bfe72fbd5">canEnumHardwareEx()</a> is called. To retrieve information about a specific channel use <a class="el" href="group___c_a_n.html#ga909e5c14b09fe76bad280475138d503e">canGetHandleData</a>(hnd, &hellip;), instead of <a class="el" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(channel, &hellip;).</p>
<dl class="section note"><dt>Note</dt><dd>On Linux, no re-enumeration is needed since enumeration takes place when a device is plugged in or unplugged. </dd></dl>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
